//
//  MGCheckNetworkStatus.m
//  CPManage
//
//  Created by lvho on 17/4/14.
//  Copyright © 2017年 com.jiaoshou.chen. All rights reserved.
//

#import "MSCheckNetworkStatus.h"
NSString *const MGNetworkStatusChangedNotification = @"MGNetworkStatusChangedNotification";
@interface MSCheckNetworkStatus ()

@property (nonatomic, assign) AFNetworkReachabilityStatus networkStatus;

@end

@implementation MSCheckNetworkStatus
/**
 开始监测网络状态,自动加载
 */
+ (void)load {
    [MSCheckNetworkStatus shareInstance];
}

+ (MSCheckNetworkStatus *)shareInstance
{
    static MSCheckNetworkStatus *share = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        share = [[MSCheckNetworkStatus alloc] init];
        
    });
    return share;
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _networkStatus = AFNetworkReachabilityStatusReachableViaWWAN;
        
        [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
            self.networkStatus = status;
            
            //发送系统通知
            [[NSNotificationCenter defaultCenter] postNotificationName:MGNetworkStatusChangedNotification object:[NSNumber numberWithInteger:self.networkStatus]];
            // 重新请求ip
            // 查询新的公网IP并保存
//            [DeviceInfo saveDeviceWANIPAddress];
        }];
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    }
    return self;
}

- (void)setNetworkStatus:(AFNetworkReachabilityStatus)networkStatus
{
    _networkStatus = networkStatus;
    
    switch (_networkStatus) {
        case AFNetworkReachabilityStatusUnknown:
        case AFNetworkReachabilityStatusNotReachable:
        {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
//                [MGMessageHint showToolMessage:@"没有网络连接"
//                                     hideAfter:2
//                                       yOffset:0
//                                    parentView:nil];
            });
            break;
        }
            
        case AFNetworkReachabilityStatusReachableViaWWAN:
        {
//            [MGMessageHint showToolMessage:@"当前是手机网络,请注意您的流量"
//                                 hideAfter:2
//                                   yOffset:0
//                                parentView:nil];
            break;
        }
            
            
        default:
            break;
    }
}

+ (BOOL)isReachable
{
    if ([MSCheckNetworkStatus isWiFiNetwork] || [MSCheckNetworkStatus isWWANNetwork]) {
        return YES;
    }
    return NO;
}

+ (AFNetworkReachabilityStatus)networkStatus
{
    return [MSCheckNetworkStatus shareInstance].networkStatus;
}

+ (BOOL)isWiFiNetwork
{
    return [MSCheckNetworkStatus networkStatus] == AFNetworkReachabilityStatusReachableViaWiFi;
}

+ (BOOL)isWWANNetwork
{
    return [MSCheckNetworkStatus networkStatus] == AFNetworkReachabilityStatusReachableViaWWAN;
}

@end
